package acwing._1_1AlgorithmBasic._3SearchGraph;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._1_1AlgorithmBasic._3SearchGraph
 * @File : Q0859.java
 * @Author : WangRuoyu
 * @Date : 2023/4/3 14:26
 */

import java.io.*;
import java.util.Arrays;

public class Q0859 {

    static int find(int x, int[] p) {
        if (p[x] == x) return x;
        else {
            int father = find(p[x], p);
            p[x] = father;
            return father;
        }
    }

    static int kruskal(int[][] edges, int[] p) {
        int res = 0;
        int cnt = 0;

        for (int[] edge : edges) {
            int fa = find(edge[1], p);
            int fb = find(edge[2], p);
            if (fa != fb) {
                p[fa] = fb;
                res += edge[0];
                cnt++;
            }
        }

        if (cnt < p.length - 1) {
            res = 0x3f3f3f3f;
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] words = br.readLine().split(" ");
        int n = Integer.parseInt(words[0]);
        int m = Integer.parseInt(words[1]);

        int[][] edges = new int[m][3];
        int[] p = new int[n];
        for (int i = 0; i < p.length; ++i) {
            p[i] = i;
        }

        for (int i = 0; i < m; ++i) {
            words = br.readLine().split(" ");
            edges[i][0] = Integer.parseInt(words[2]);
            edges[i][1] = Integer.parseInt(words[0]) - 1;
            edges[i][2] = Integer.parseInt(words[1]) - 1;
        }

        Arrays.sort(edges, (o1, o2) -> o1[0] != o2[0] ? o1[0] - o2[0] : o1[1] - o2[1]);

        int res = kruskal(edges, p);

        if (res == 0x3f3f3f3f) {
            System.out.println("impossible");
        } else {
            System.out.println(res);
        }
    }
}

